package com.cl.blog.web.client;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cl.blog.annotation.Login;
import com.cl.blog.common.Const;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BCollect;
import com.cl.blog.entity.BContent;
import com.cl.blog.entity.BUser;
import com.cl.blog.entity.BUserFollow;
import com.cl.blog.service.BCollectService;
import com.cl.blog.service.BContentService;
import com.cl.blog.service.BUserFollowService;
import com.cl.blog.service.BUserService;
import com.cl.blog.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 付
 * @Date 2024-04-01 18:59
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RequestMapping("/user")
@RestController
public class UserController {

	@Autowired
	BUserService bUserService;
	@Autowired
	BUserFollowService bUserFollowService;
	@Autowired
	JwtUtil jwtUtil;
	@Autowired
	BCollectService bCollectService;
	@Autowired
	BContentService bContentService;


	// 根据具体文章获取用户信息
	@GetMapping("/one")
	@Login
	public Result getOne(Integer contentId, Integer id, HttpServletRequest request) {
		String token = request.getHeader("token");
		Map<Object, Object> map = bUserService.getUserInfo(contentId, id, token);
		return Result.success(map);
	}


	/**
	 * 用户点赞
	 *
	 * @param contentId
	 * @param request
	 * @return
	 */
	@GetMapping("/like")
	@Login
	public Result userLike(Integer contentId, HttpServletRequest request) {
		String token = request.getHeader("token");
		int b = bUserService.like(contentId, token);
		return Result.success(b);
	}

	/**
	 * 用户收藏文章
	 *
	 * @param contentId 文章id
	 * @param request
	 * @return
	 */
	@GetMapping("/collect")
	@Login
	public Result userCollect(Integer contentId, HttpServletRequest request) {
		String token = request.getHeader("token");
		int b = bUserService.collect(contentId, token);
		return Result.success(b);
	}


	/**
	 * 用户关注
	 *
	 * @param userId  被关注的用户id
	 * @param request
	 * @return
	 */
	@GetMapping("/follow")
	@Login
	public Result follow(Integer userId, HttpServletRequest request) {

		String token = request.getHeader("token");
		boolean b = bUserFollowService.follow(userId, token);
		return Result.success(b);
	}

	/**
	 * 根据用户id获取所有信息
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/userinfo")
	@Login
	public Result getUserInfo(Integer userId, HttpServletRequest request) {
		System.out.println("请求：userinfo-->" + userId);
		Object bUser = new Object();
		if (userId == null) {
			String token = request.getHeader("token");
			Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_USER);
			String subject = claim.getSubject();
			bUser = bUserService.getUser(Integer.valueOf(subject), request);
		} else {
			bUser = bUserService.getUser(userId, request);
		}
		return Result.success(bUser);
	}


	@GetMapping("/collected")
	@Login
	public Result getUserFollowed(Integer userId) {
		List<BCollect> bCollects = bCollectService.list(new QueryWrapper<BCollect>().eq("user_id", userId));
		List<Integer> collect = bCollects.stream().map(BCollect::getContentId).collect(Collectors.toList());
		// collect会存在为空的情况
		if (collect.size() <= 0) {
			return Result.success(new Page<BContent>());
		}
		Page<BContent> page = bContentService.page(new Page<>(0, 20), new QueryWrapper<BContent>()
				.eq("status", 2)
				.eq("is_delete", 0)
				.in("id", collect)
		);

		return Result.success(page);
	}

	@GetMapping("/delete")
	public Result delete(Integer id, Integer status, HttpServletRequest request) {
		// 1 删除文章，2 删除收藏，3 删除点赞
		boolean b = bUserService.deleteByIdAndStatus(id, status, request);

		if (!b) {
			return Result.error("系统错误，稍后再试");
		}
		return Result.success();
	}

	@PostMapping("/update")
	public Result updateUser(@RequestBody BUser bUser) {
		bUser.setPassword(SecureUtil.md5(bUser.getPassword()));
		boolean id = bUserService.update(bUser, new UpdateWrapper<BUser>().eq("id", bUser.getId()));
		if (!id) {
			return Result.error("修改失败");
		}
		return Result.success();
	}
}
